<template>
  <div class="stock">
    <el-form :inline="true" :model="form">
      <el-form-item label="仓库名" :label-width="formLabelWidth">
        <el-input v-model="listQuery.repoName" autocomplete="off"></el-input>
      </el-form-item>
      <div></div>
      <el-button type="success" icon="el-icon-zoom-in" @click="getStockList()"
        >查询</el-button
      >
      <el-button type="primary" icon="el-icon-plus" @click="carAdd = true"
        >添加</el-button
      >
    </el-form>

    <el-dialog
      title="汽车添加"
      :visible.sync="carAdd"
      width="500px"
      :before-close="handleClose"
    >
      <el-form :model="repository" ref="cargoFormEdit">
        <el-form-item
          :required="true"
          label="仓库地址"
          :label-width="formLabelWidth"
        >
          <el-input
            v-model="repository.repoAddress"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          :required="true"
          label="仓库名"
          :label-width="formLabelWidth"
        >
          <el-input v-model="repository.repoName" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="AddCarList()">确 定</el-button>
      </span>
    </el-dialog>

    <el-table
      @current-change="handleSelectionChange"
      highlight-current-row
      :data="stockList"
      border
      style="width: 620px; margin: 20px auto"
    >
      <el-table-column
        prop="repoAddress"
        label="地点"
        width="200"
      ></el-table-column>
      <el-table-column
        prop="repoName"
        label="仓库名"
        width="200"
      ></el-table-column>

      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="danger"
            @click="removeBigOrders(scope.$index, scope.row)"
            >删除</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="PutBigOrders(scope.$index)"
            >修改</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      title="修改仓库信息"
      :visible.sync="putCar"
      width="400px"
      :before-close="handleClose"
    >
      <el-form :model="putList">
        <el-form-item :required="true" label="仓库名" label-width="200">
          <el-input v-model="putList.repoName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item :required="true" label="仓库地址" label-width="200">
          <el-input v-model="putList.repoAddress" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="putCarList()">修改</el-button>
      </span>
    </el-dialog>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        :hide-on-single-page="true"
        @current-change="handleCurrentChange"
        :current-page="listQuery.pageNo"
        :page-sizes="[10, 50, 100]"
        :page-size="listQuery.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      stockList: [],
      checkCarList: {
        id: "",
      },
      stockTextList: [],
      carAdd: false,
      total: 0,
      putCar: false,
      formLabelWidth: "120px",
      listQuery: {
        pageSize: 10,
        pageNo: 1,
        repoName: "",
      },
      putList: [],
      repository: {
        repoAddress: "",
        repoName: "",
      },
    };
  },
  created() {
    this.getStockList();
  },
  methods: {
    getStockList() {
      let token = window.localStorage.getItem("token");
      this.$axios({
        method: "get",
        url: this.GLOBAL.checkStock,
        params: this.listQuery,
        headers: { "X-Access-Token": token },
      })
        .then((result) => {
          this.stockList = result.data.result.records;
          this.total = result.data.result.total;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    getCarCheckText() {
      let token = window.localStorage.getItem("token");
      this.$axios({
        method: "get",
        url: this.GLOBAL.getCheckStock,
        params: this.checkCarList,
        headers: { "X-Access-Token": token },
      })
        .then((result) => {
          this.carTextList = result.data.result.carInfoList;
          console.log(this.carTextList);
          this.carText = true;
        })
        .catch((err) => {
          console.log(err);
        });
    },
    AddCarList() {
      let token = window.localStorage.getItem("token");
      this.$axios({
        method: "post",
        url: this.GLOBAL.addStock,
        data: this.repository,
        headers: { "X-Access-Token": token },
      })
        .then((result) => {
          if (result.data.code == 200 || result.data.code == "200") {
            this.$message({
              type: "success",
              message: result.data.message,
            });
            this.carAdd = false;
            this.getStockList();
          } else {
            this.$message({
              type: "warning",
              message: result.data.message,
            });
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    PutBigOrders(index) {
      this.putList = this.stockList[index];
      console.log(this.putList);
      this.putCar = true;
    },
    putCarList() {
      let token = window.localStorage.getItem("token");
      this.putList.carInfoList = [{}];
      this.$axios({
        method: "post",
        url: this.GLOBAL.addStock,
        data: this.putList,
        headers: { "X-Access-Token": token },
      })
        .then((result) => {
          if (result.data.code == 200 || result.data.code == "200") {
            this.$message({
              type: "success",
              message: result.data.message,
            });
            this.putCar = false;
            this.getStockList();
          } else {
            this.$message({
              type: "warning",
              message: result.data.message,
            });
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    removeBigOrders(index, row) {
      console.log(index, row);
      let ids = this.stockList[index].repoId;
      let token = window.localStorage.getItem("token");
      this.$axios({
        method: "delete",
        url: this.GLOBAL.deleteStock,
        params: { ids: ids },
        headers: { "X-Access-Token": token },
      })
        .then((result) => {
          console.log(result);
          if (result.data.code == 200 || result.data.code == "200") {
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            this.getStockList();
          } else {
            this.$message({
              type: "warning",
              message: "删除失败!",
            });
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    handleSizeChange(val) {
      this.listQuery.pageSize = val;
      this.getStockList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNo = val;
      this.getStockList();
    },
    handleSelectionChange(val) {
      console.log(val);
      this.checkCarList.id = val.repoId;
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          done();
        })
        .catch(() => {});
    },
  },
};
</script>

<style>
</style>